package com.yingpai.fitness.dvp_2_0.main.activity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.SharedPreferencesHelp;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.callback.WechatCallBack;
import com.yingpai.fitness.dvp_2_0.main.util.WechatLoginUtil;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.wx.WxLoginBean;

import java.math.BigInteger;

import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by ASUS on 2018/11/5.
 */

public class LoginAndRegisterActivity extends CommonActivity implements WechatCallBack {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.register_and_login_flag_activity);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    @OnClick({R.id.login, R.id.register, R.id.wechat_login})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.login:
                startActivity(LoginActivity.class);
                break;
            case R.id.register:
                startActivity(RegisterActivity.class);
                break;
            case R.id.wechat_login:
                WechatLoginUtil.getInstance().setWechatCallBack(this);
                WechatLoginUtil.getInstance().wechatLogin();
                break;
        }
    }

    @Override
    public void callBack(BaseResp baseResp) {
        if (baseResp instanceof SendAuth.Resp) {
            SendAuth.Resp newResp = (SendAuth.Resp) baseResp;
            if (newResp.state.equals("wechat_sdk_login")) {
                HttpParams httpParams = new HttpParams();
                httpParams.put("code", newResp.code);
                showDialog(this, "登录中...");
                finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.WX_LOGIN, httpParams, Url.WX_LOGIN, new Callback<WxLoginBean>() {
                    @Override
                    public void onSuccess(WxLoginBean wxLoginBean) {
                        dismissDialog();
                        if (wxLoginBean.getCode().equals(Constant.REQUEST_SUCCESS_0)) {
                            if (wxLoginBean.getData().getData().getFlag().equals(Constant.REQUEST_SUCCESS_0)) {
                                Bundle bundle = new Bundle();
                                bundle.putString("infoUserWxId", wxLoginBean.getData().getData().getInfoUserWxId());
                                startActivity(WeChatBindPhoneActivity.class, bundle);
                            } else {
                                wxLoginBean.getData().getData().setRegisterId(JPushInterface.getRegistrationID(context));
                                SharedPreferencesHelp.putBean(Constant.TOKENBEAN, wxLoginBean.getData().getData());
                                JPushInterface.setAlias(context,wxLoginBean.getData().getData().getId(),"妖动动" + wxLoginBean.getData().getData().getId());
                                startFinishActivity(HomeActivity.class);
                            }
                        } else {
                            showToast(wxLoginBean.getMessage());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        dismissDialog();
                    }
                });
            }
        }
    }
}
